﻿define([
    'dojo/_base/declare',
    'esri/geometry/Point',
    'geomap/plugins/layers/VacuateLayer',
], function (declare, Point, VacuateLayer) {
    var CustomGraphicDivLayer = declare([VacuateLayer], {
        setData: function (mapView, layer) {
            var me = this;
            me._mapView = mapView;
            var mapUtils = mapView.mapUtils;
            var options = layer.layer || {};
            var data = $.extend(true, [], options.data);

            me.options = options;
            /*数据源超过这个数据，才开始聚类*/
            me._thresholdValue = options.thresholdValue || 0;
            /*地图比例尺小于这个数据，不再聚类，全部散开*/
            me._clusterUpperLimitScale = options.clusterUpperLimitScale || 0;
            /*地图级别大于这个数据，不再聚类，全部散开*/
            me._clusterUpperLimitLevel = options.clusterUpperLimitLevel || 0;
            var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
            var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();
            me.__getSymbol = mapUtils.getSymbol.bind(mapUtils);
            var symbol = options[symbolFieldName] || 'common_point';
            mapUtils.getSymbol(mapView, symbol, function (symbol) {
                me._clusterData = lngLatArray || [];
                me._symbol = symbol;
                me._setAddGraphics();
                me.setLabel(mapView, options);
                if (typeof options.callback == 'function') {
                    options.callback(layer);
                }
            });
        },
        getGraphicLocation: function (mapView, esriEvent) {
            var geometry = esriEvent.graphic.geometry;
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.x;
            location[latFieldName] = geometry.y;
            return location;
        },
        setLabel: function (mapView, options) {
            var me = this;
            var mapUtils = mapView.mapUtils;
            if (options.labelOptions) {
                me.labelOptions = mapUtils.$.extend(
                    true,
                    {
                        textField: 'name',
                        ptThreshold: 0, //当前窗口的点位少于这个最大值时，标签强制展示
                        showLabels: 1, //1:根据比例尺展示，2：强制展示，0：强制不展示
                        scaleInfo: {
                            maxScale: 0, // 1000000,
                            minScale: 0,
                        },
                        labelSymbol: {
                            labelClass: 'labelInfo',
                            labelStyle: { offsety: 18, offsetx: 0, position: 'top' },
                        },
                    },
                    options.labelOptions
                );
                me._showLabels(mapView, mapUtils, options);
            }
        },
    });
    return CustomGraphicDivLayer;
});
